# -*- coding: utf-8 -*-

'''Database manager.'''

__author__ = 'mengyanwei'

from collections import OrderedDict
from pymongo import MongoClient
from pymongo.database import Database

class BaseDBer(Database):
    '''Basic database manager.'''
    pass

class MongoDBer(BaseDBer):
    '''Mongodb manager.'''

    def __init__(self, conf, **kwargs):
        ''' conf: a dict type, contain basic info
        '''
        assert isinstance(conf, dict)

        self.host = conf['host']
        self.port = conf['port']
        self.database = conf['database']
        self.user = conf['user']
        self.password = conf['password']
        try:
            self.connection = MongoClient(self.host, self.port, document_class=OrderedDict, **kwargs)
            super(MongoDBer, self).__init__(self.connection, self.database)
            self.authenticate(name=self.user, password=self.password)
        except Exception as  err:
            print('mongodb 连接中断 重新连接')
            self.connection = MongoClient(self.host, self.port, document_class=OrderedDict, **kwargs)
            super(MongoDBer, self).__init__(self.connection, self.database)
            self.authenticate(name=self.user, password=self.password)

    def __del__(self):
        pass
